import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class test {
	public int n = 0;
	static List<String[]> proxies = new ArrayList();
	static int usedProxies = 0;
	static String video;
	static double len;
	static String build_id = "";
	static String referer = "";
	static test test;

	public static void main(String[] args) throws InterruptedException,
			IOException {
		if (args.length > 3) {
			video = args[0];
			len = Double.valueOf(args[1]).doubleValue();
			String proxyFile = args[3];
			if (args.length > 4) {
				referer = args[4];
			}

			Document resp = Jsoup
					.connect("http://m.youtube.com/watch")
					.timeout(10000)
					.ignoreHttpErrors(true)
					.data("v", video)
					.userAgent(
							"Mozilla/5.0 (Linux; Android 4.2.2; Nexus 4 Build/JDQ39) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.64 Mobile Safari/537.36")
					.get();
			Pattern build_idPattern = Pattern
					.compile("build_id: (.*), content");
			Matcher m = build_idPattern.matcher(resp.getElementsByTag("script")
					.html().replace("\\\"", ""));
			while (m.find()) {
				build_id = m.group(1);
			}

			System.out.println("build_id=" + build_id);

			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(new File(proxyFile))));
			String strLine;
			while ((strLine = br.readLine()) != null) {
				// String strLine;
				if (!strLine.isEmpty()) {
					proxies.add(strLine.split(":"));
				}
			}
			br.close();

			test = new test();

			for (int i = 0; i < Integer.valueOf(args[2]).intValue(); i++)
				if (i < proxies.size()) {
					threadTest tt = new threadTest(video, len, build_id,
							(String[]) proxies.get(i), test);
					tt.referrer = referer;
					new Thread(tt).start();
					usedProxies += 1;
				} else {
					threadTest tt = new threadTest(video, len, build_id, null,
							test);
					tt.referrer = referer;
					new Thread(tt).start();
				}
		} else {
			System.out
					.println("Correct syntax: java -jar youtubeMobileViews.jar VIDEO_ID leng.th threads C:/path/proxies.txt referer");
			Thread.sleep(3000L);
		}
	}

	public void fireNewThread() {
		if (usedProxies < proxies.size()) {
			threadTest tt = new threadTest(video, len, build_id,
					(String[]) proxies.get(usedProxies), test);
			tt.referrer = referer;
			new Thread(tt).start();
			usedProxies += 1;
		} else {
			usedProxies = 0;
			if (proxies.size() > 0) {
				threadTest tt = new threadTest(video, len, build_id,
						(String[]) proxies.get(usedProxies), test);
				tt.referrer = referer;
				new Thread(tt).start();
				usedProxies += 1;
			} else {
				threadTest tt = new threadTest(video, len, build_id, null, test);
				tt.referrer = referer;
				new Thread(tt).start();
			}
		}
	}
}